﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;
using System.IO;
using System.Reflection;

namespace CEQuery
{
    public partial class MainForm : Form
    {
        //private int childFormNumber = 0;
        public string ConnectionString { get; set; }
        public MainForm(string[] args)
        {
            InitializeComponent();
            this.Text += String.Format(" [Version {0} with SQL CE {1}]", Assembly.GetExecutingAssembly().GetName().Version.ToString(), Assembly.GetAssembly(typeof(SqlCeConnection)).GetName().Version.ToString());

            if (args != null && args.Length > 0)
            {
                OpenDB dbOpen = new OpenDB(args[0]);
                if (dbOpen.ShowDialog() == DialogResult.OK)
                {
                    ConnectionString = dbOpen.ConnectionString;
                    using (SqlCeConnection connection = new SqlCeConnection(ConnectionString))
                    {
                        try
                        {
                            connection.Open();
                            this.toolStripStatusLabel.Text = "DB selected successfully.";
                            FormQuery frmQuery = new FormQuery();
                            frmQuery.MdiParent = this;
                            frmQuery.ConnectionString = this.ConnectionString;
                            frmQuery.Show();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                        finally
                        {
                            connection.Close();
                        }
                    }
                }
            }
        }

        private void ShowNewForm(object sender, EventArgs e)
        {            
            OpenDB dbOpen = new OpenDB();
            if (dbOpen.ShowDialog() == DialogResult.OK)
            {
                ConnectionString = dbOpen.ConnectionString;
                using (SqlCeConnection connection = new SqlCeConnection(ConnectionString))
                {
                    try
                    {
                        connection.Open();
                        //MessageBox.Show("DB Selected");
                        this.toolStripStatusLabel.Text = "DB selected successfully.";
                        //foreach (Form childForm in MdiChildren)
                        //{
                        //    childForm.Close();
                        //}
                        FormQuery frmQuery = new FormQuery();
                        frmQuery.MdiParent = this;
                        frmQuery.ConnectionString = this.ConnectionString;
                        //frm.Text = openFileDialogue.FileName;
                        frmQuery.Show();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    finally
                    {
                        connection.Close();
                    }
                }
            }
       
        }                       

        private void selectEngagementDBToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowNewForm(this, e);
        }

        private void saveToolStripButton_Click(object sender, EventArgs e)
        {
            
            if (((System.Windows.Forms.Form[])this.MdiChildren).Length > 0 && this.ActiveMdiChild.GetType() == typeof(FormQuery))
            {
                if (((FormQuery)this.ActiveMdiChild).SaveActiveQuery())
                {
                    this.toolStripStatusLabel.Text = "Query has been saved successfully.";
                }
            }
            else
            {
                MessageBox.Show("Open a DB first.");
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        
        private void toolStripRunCommand_Click(object sender, EventArgs e)
        {
            if (((System.Windows.Forms.Form[])this.MdiChildren).Length > 0 && this.ActiveMdiChild.GetType() == typeof(FormQuery))
            {
                ((FormQuery)this.ActiveMdiChild).RunActiveQuery();
            }
            else
            {
                MessageBox.Show("Open a DB first.");
            }
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CEAboutBox frm = new CEAboutBox();
            frm.ShowDialog();
        }

        private void createDBToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CreateDB dbCreate = new CreateDB();
            if (dbCreate.ShowDialog() == DialogResult.OK)
            {
                //foreach (Form childForm in MdiChildren)
                //{
                //    childForm.Close();
                //}
                this.toolStripStatusLabel.Text = "DB has been created successfully.";
            }
        }

        private void newQueryToolStripButton_Click(object sender, EventArgs e)
        {
            if (((System.Windows.Forms.Form[])this.MdiChildren).Length > 0 && this.ActiveMdiChild.GetType() == typeof(FormQuery))
            {
                ((FormQuery)this.ActiveMdiChild).AddQueryTab();
                this.toolStripStatusLabel.Text = "A new query tab has been added.";
            }
            else
            {
                MessageBox.Show("Open a DB first.");
            }
        }

        private void toolStripButtonScriptGenerator_Click(object sender, EventArgs e)
        {
            if (((System.Windows.Forms.Form[])this.MdiChildren).Length > 0 && this.ActiveMdiChild.GetType() == typeof(FormQuery))
            {
                ScriptGenerator frmScript = new ScriptGenerator();
                //frmScript.ConnectionString = this.ConnectionString;
                frmScript.ConnectionString = ((FormQuery)this.ActiveMdiChild).ConnectionString;
                if (frmScript.ShowDialog() == DialogResult.OK)
                {
                    this.toolStripStatusLabel.Text = "Script has been generated successfully.";
                }
            }
            else
            {
                MessageBox.Show("Open a DB first.");
            }
        }

        private void exportSqlToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ExportSqlServer sqlExport = new ExportSqlServer();
            sqlExport.ShowDialog();
        }

        private void changePasswordToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ChangePasswordDB chanePwd = new ChangePasswordDB();
            if (chanePwd.ShowDialog() == DialogResult.OK)
            {
                if (chanePwd.OldConnectionString != string.Empty && chanePwd.NewConnectionString != string.Empty)
                {
                    foreach (Form frm in this.MdiChildren)
                    {
                        if (frm.GetType() == typeof(FormQuery) && ((FormQuery)frm).ConnectionString == chanePwd.OldConnectionString)
                        {
                            ((FormQuery)frm).ConnectionString = chanePwd.NewConnectionString;
                        }
                    }
                }
            }
        }

        private void toolStripButtonDQE_Click(object sender, EventArgs e)
        {
            if (((System.Windows.Forms.Form[])this.MdiChildren).Length > 0 && this.ActiveMdiChild.GetType() == typeof(FormQuery))
            {
                DQE frmDqe = new DQE();
                frmDqe.CETables = ((FormQuery)this.ActiveMdiChild).CETables;
                if (frmDqe.ShowDialog() == DialogResult.OK)
                {
                    ((FormQuery)this.ActiveMdiChild).AddInsertQueryTab(frmDqe.Query);
                }
            }
            else
            {
                MessageBox.Show("Open a DB first.");
            }
        }

        private void validateDBToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (((System.Windows.Forms.Form[])this.MdiChildren).Length > 0 && this.ActiveMdiChild.GetType() == typeof(FormQuery))
            {
                ValidateDB validateDb = new ValidateDB();
                validateDb.ConnectionString = ((FormQuery)this.ActiveMdiChild).ConnectionString;
                if (validateDb.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    this.ActiveMdiChild.Close();
                }
            }
            else
            {
                MessageBox.Show("Open a DB first.");
            }
        }

        private void compactDBToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (((System.Windows.Forms.Form[])this.MdiChildren).Length > 0 && this.ActiveMdiChild.GetType() == typeof(FormQuery))
                {
                    if (MessageBox.Show(this, "Are you sure to compact the DB?", "CEQuery", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                    {
                        using (SqlCeConnection localConnection = new SqlCeConnection(((FormQuery)this.ActiveMdiChild).ConnectionString))
                        {
                            using (SqlCeEngine engine = new SqlCeEngine(((FormQuery)this.ActiveMdiChild).ConnectionString))
                            {
                                engine.Compact(((FormQuery)this.ActiveMdiChild).ConnectionString);
                            }
                            MessageBox.Show("DB has been compacted successfully. Please reload the DB.");
                            this.ActiveMdiChild.Close();
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Open a DB first.");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void shrinkDBToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (((System.Windows.Forms.Form[])this.MdiChildren).Length > 0 && this.ActiveMdiChild.GetType() == typeof(FormQuery))
                {
                    if (MessageBox.Show(this, "Are you sure to shrink the DB?", "CEQuery", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                    {
                        using (SqlCeConnection localConnection = new SqlCeConnection(((FormQuery)this.ActiveMdiChild).ConnectionString))
                        {
                            using (SqlCeEngine engine = new SqlCeEngine(((FormQuery)this.ActiveMdiChild).ConnectionString))
                            {
                                engine.Shrink();
                            }
                            MessageBox.Show("DB has been shrinked successfully. Please reload the DB.");
                            this.ActiveMdiChild.Close();
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Open a DB first.");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
